#include "uibaseimpl.h"
#include <utilex/getpath.hpp>

CUiBaseImpl::CUiBaseImpl(void)
{

}
CUiBaseImpl::~CUiBaseImpl(void)
{

}
//////////////////////////////////////////////////////////////////////////
std_method_impl CUiBaseImpl::init_class(IBase* prot, IBase* punkOuter)
{
	HRESULT		hr = S_OK;
	if (prot != NULL)
	{
		m_pRot = prot;
	}
	return hr;
}
std_method_impl CUiBaseImpl::Init(void*)
{
	HRESULT		hr = S_OK;
	os_sem_init(&this->m_sem, 0);
	return hr;
}
std_method_impl CUiBaseImpl::Uninit()
{
	HRESULT		hr = S_OK;
	os_sem_destroy(&this->m_sem);
	return hr;
}
std_method_impl CUiBaseImpl::Start()
{
	HRESULT		hr = S_OK;

	m_pRot->GetObject(CLSID_IHttp, _uuidof(IHttp), (IBase**)&m_pIHttp.m_p);
	retrtpv(m_pIHttp, E_FAIL)

	m_pIHttp->CreateHttp_t((IBase**)&m_pIHttpClient.m_p);
	retrtpv(m_pIHttpClient, E_FAIL)

	m_pRot->GetObject(CLSID_IAsynFrame, _uuidof(IAsynIo), (IBase**)&m_pIAsynIo.m_p);
	retrtpv(m_pIAsynIo, E_FAIL)

	m_pIAsynIo->CreateWriteFile((IBase**)&m_pIWriteFile.m_p);
	retrtpv(m_pIWriteFile, E_FAIL)


	this->Create(NULL);

	iosprintf("CUiBaseImpl::Start");
	return hr;
}
std_method_impl CUiBaseImpl::Stop()
{
	HRESULT		hr = S_OK;

	iosprintf("CUiBaseImpl::Stop");
	return hr;
}

std_method_type_impl(HWND) CUiBaseImpl::Create(HWND hParentHwnd)
{
	HWND hWnd = NULL;
	//http://dldir1.qq.com/music/clntupate/QQMusicSetup.exe
	//https://qd.myapp.com/myapp/qqteam/pcqq/QQ9.0.8_3.exe

	tstring stringpath = GetCurrentRunPath(NULL);
	stringpath += path_slash;
	stringpath += _T("1.exe");

	m_pIWriteFile->LoadFile(stringpath.c_str(), 0, 0, 0, 0);
	m_pIHttpClient->SetNotify(this);
	m_pIHttpClient->Perform("127.0.0.1","GET /1.exe HTTP/1.0", 80);
	os_sem_wait(&this->m_sem);
	//m_pIHttpClient->ReSet();

	return hWnd;
}
std_method_impl CUiBaseImpl::Destroy()
{
	HRESULT		hr = S_OK;

	return hr;
}
std_method_type_impl(BOOL) CUiBaseImpl::Show(int nCmdShow)
{
	return true;
}
//////////////////////////////////////////////////////////////////////////
//IHttpNotify
ULONG CUiBaseImpl::Event(ULONG ulevent)
{
	if (HTTP_IMPL_CONNECTED == ulevent) {
		return HTTP_IMPL_SEND_HEAD;
	}
	else if (HTTP_IMPL_SEND_HEADED == ulevent) {
		return HTTP_IMPL_RECV_HEAD;
	}
	else if (HTTP_IMPL_RECV_HEADED == ulevent) {
		return HTTP_IMPL_RECV_BODY;
	} 
	else if (HTTP_IMPL_RECV_BODYED == ulevent) {
		return HTTP_IMPL_RECV_BODY;
	}
	else if (ulevent == HTTP_IMPL_END) {
		os_sem_post(&this->m_sem);
		iosprintf("http end");
		return ulevent;
	}
	else {
		return OnEventError(ulevent);
	}
	return HTTP_IMPL_UNKNOWN;
}
ULONG CUiBaseImpl::SendHead(void* data, ULONG nSize, size_t nLen)
{
	char* p = (char*)data;

	return nLen;
}
ULONG CUiBaseImpl::SendBody(void* data, ULONG nSize, size_t nLen)
{
	return nSize;
}
ULONG CUiBaseImpl::RecvHead(void* data, ULONG nSize, size_t nLen)
{
	return nSize;
}
ULONG CUiBaseImpl::RecvBody(void* data, ULONG nSize, size_t nLen)
{
	if (m_pIWriteFile != INULL) {

		char szlog[1024] = { 0x00 };
		sprintf(szlog, "write file %lu", nSize);
		iosprintf(szlog);
		m_pIWriteFile->WriteBuffer((char*)data, nSize);
	}
	return nSize;
}
//////////////////////////////////////////////////////////////////////////
ULONG CUiBaseImpl::OnEventError(ULONG ulevent)
{
	os_sem_post(&this->m_sem);

	return ulevent;
}
